package com.ziq.zcamp.community

import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.blankj.utilcode.util.BarUtils.getStatusBarHeight
import com.ziq.base.base.BaseFragment
import com.ziq.base.extension.binding
import com.ziq.base.extension.click
import com.ziq.zcamp.R
import com.ziq.zcamp.databinding.FragmentCommunityBinding
import com.ziq.zcamp.main.MainFragment
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.manager.PrivacyManager
import com.ziq.zcamp.place.PlaceListFragment
import com.ziq.zcamp.search.SearchMainActivity
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class CommunityFragment : BaseFragment(R.layout.fragment_community){

    private val binding : FragmentCommunityBinding? by binding()

    private val fragments: MutableList<Pair<Int, Fragment>> = mutableListOf()
    private var current = 0

    enum class Tab {
        CommunityTab(10),
        CommunityActivities(11);

        var value:Int

        constructor(value: Int){
            this.value = value
        }
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding?.root

        binding?.container?.setPadding(0, getStatusBarHeight(), 0,0)

        fragments.clear()
        addFragment(fragments, Tab.CommunityTab.value, CommunityTabFragment::class.java, createAction = {
            CommunityTabFragment.createFragment()
        })
        addFragment(fragments, Tab.CommunityActivities.value, CommunityActivitiesFragment::class.java,  createAction = {
            CommunityActivitiesFragment()
        })

        binding?.viewPager?.offscreenPageLimit = 2
        binding?.viewPager?.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int {
                return fragments.size
            }

            override fun createFragment(position: Int): Fragment {
                return fragments[position].second
            }
        }
        binding?.viewPager?.isUserInputEnabled = false

        binding?.tvActivities?.click {
            GlobalContextManager.getCurrentActivity()?.let {
                PrivacyManager.checkPrivacy(it, action = {
                    changeCurrentFragment(1)
                })
            }
        }
        binding?.tvRecommend?.click {
            changeCurrentFragment(0)
        }
        binding?.ivSearch?.click {
            SearchMainActivity.open(context=requireContext(),SearchMainActivity.SOURCE_FROM_DYNAMIC)
        }
    }

    private fun addFragment(fragments: MutableList<Pair<Int, Fragment>>, key: Int, cls : Class<*>, createAction : ()->Fragment){
        var fragment = childFragmentManager.fragments.firstOrNull { it::class.java.canonicalName == cls.canonicalName }
        if(fragment == null){
            fragment = createAction.invoke()
        }
        fragments.add(Pair(key, fragment))
    }

    private fun changeCurrentFragment(index : Int){
        if (index !in 0..1 || index == current){
            return
        }
        binding?.viewPager?.currentItem = index
        if (index == 1 ){
            binding?.container?.setBackgroundColor(requireContext().resources.getColor(R.color.color_20211f))
            fontAnimation(true)
        }else{
            binding?.container?.setBackgroundColor(requireContext().resources.getColor(R.color.white))
            fontAnimation(false)
        }
        current = index

    }

    /**
     * 字体切换的动画
     */
    private fun fontAnimation(recommendToActivity:Boolean){
        if (recommendToActivity){
            val recommendedAnimation : ValueAnimator = ValueAnimator.ofArgb(resources.getColor(R.color.color_333333),resources.getColor(R.color.color_afafaf))
            recommendedAnimation.addUpdateListener {
                val value = it.animatedValue as Int
                binding?.tvRecommend?.setTextColor(value)
            }
            val activitiesAnimator : ValueAnimator = ValueAnimator.ofArgb(resources.getColor(R.color.color_afafaf),resources.getColor(R.color.white))
            activitiesAnimator.addUpdateListener {
                val value = it.animatedValue as Int
                binding?.tvActivities?.setTextColor(value)
            }
            val transparencyAnimator : ValueAnimator = ValueAnimator.ofFloat(0F,1F)
            transparencyAnimator.addUpdateListener {
                val value = it.animatedValue as Float
                binding?.bgRecommend?.alpha = (1.0 - value).toFloat()
                binding?.indicatorRecommend?.alpha = (1.0 - value).toFloat()
                binding?.bgActivities?.alpha = value
                binding?.indicatorActivities?.alpha = value
            }
            val sizeAnimator = ValueAnimator.ofFloat(0F,2F)
            sizeAnimator.addUpdateListener {
                val value = it.animatedValue as Float
                binding?.tvActivities?.setTextSize(TypedValue.COMPLEX_UNIT_DIP,18F + value)
                binding?.tvRecommend?.setTextSize(TypedValue.COMPLEX_UNIT_DIP,20F - value)
            }
            val set = AnimatorSet()
            set.duration = 250
            set.playTogether(recommendedAnimation,activitiesAnimator,transparencyAnimator,sizeAnimator)
            set.start()
        }else{
            val recommendedAnimation : ValueAnimator = ValueAnimator.ofArgb(resources.getColor(R.color.color_afafaf),resources.getColor(R.color.color_333333))
            recommendedAnimation.addUpdateListener {
                val value = it.animatedValue as Int
                binding?.tvRecommend?.setTextColor(value)
            }
            val activitiesAnimator : ValueAnimator = ValueAnimator.ofArgb(resources.getColor(R.color.white),resources.getColor(R.color.color_afafaf))
            activitiesAnimator.addUpdateListener {
                val value = it.animatedValue as Int
                binding?.tvActivities?.setTextColor(value)
            }
            val transparencyAnimator : ValueAnimator = ValueAnimator.ofFloat(0F,1F)
            transparencyAnimator.addUpdateListener {
                val value = it.animatedValue as Float
                binding?.bgActivities?.alpha = (1.0 - value).toFloat()
                binding?.indicatorActivities?.alpha = (1.0 - value).toFloat()
                binding?.bgRecommend?.alpha = value
                binding?.indicatorRecommend?.alpha = value
            }
            val sizeAnimator = ValueAnimator.ofFloat(0F,2F)
            sizeAnimator.addUpdateListener {
                val value = it.animatedValue as Float
                binding?.tvRecommend?.setTextSize(TypedValue.COMPLEX_UNIT_DIP,18F + value)
                binding?.tvActivities?.setTextSize(TypedValue.COMPLEX_UNIT_DIP,20F - value)
            }
            val set = AnimatorSet()
            set.duration = 250
            set.playTogether(recommendedAnimation,activitiesAnimator,transparencyAnimator,sizeAnimator)
            set.start()
        }

        if (recommendToActivity){
            binding?.ivSearch?.setImageResource(R.drawable.icon_main_search_white)
        }else{
            binding?.ivSearch?.setImageResource(R.drawable.icon_main_search)
        }
    }

}